package com.octopus.security;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.log4j.Logger;

import com.octopus.string.StringUtils;

public class EnDeCrypt {
	
	private static final Logger logger = Logger.getLogger(EnDeCrypt.class.getName());
	
    Key key;

    public EnDeCrypt(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            /*
             * Here we force to use "SHA1PRNG", because when we change to UNIX, its may be "NativePRNG".
             * It will make the result very different between Windows and HP UNIX.
             */
            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(strKey.getBytes());
            _generator.init(sr);
            this.key = _generator.generateKey();
            _generator = null;
        } catch (Exception e) {
        	logger.warn("EnDeCrypt constructor",e);
        }
    }

    public String encString(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = null;
        try {
            byteMing = strMing.getBytes("UTF8");
            byteMi = this.getEncCode(byteMing);
            strMi = StringUtils.base64en(byteMi);
        } catch (Exception e) {
            logger.warn("encString", e);
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMi;
    }

    public String desString(String strMi) {
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = null;
        try {
            byteMi = StringUtils.base64de(strMi);
            byteMing = this.getDesCode(byteMi);
            strMing = new String(byteMing, "UTF8");
        } catch (Exception e) {
        	logger.warn("desString", e);
        } finally {
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }

    private byte[] getEncCode(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            logger.warn("getEncCode", e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    
    private byte[] getDesCode(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            logger.warn("getDesCode", e);
        } finally {
            cipher = null;
        }
        return byteFina;
    }
   
    public static void main(String[] args) throws Exception{
    	EnDeCrypt des = new EnDeCrypt("com.libset.db.derby.DerbyTool.20110408.KEY");
    	String strEnc = des.encString("dba,Alufengsi");
    	String password = des.encString("2wsx3edc");
    	System.out.println("user:["+strEnc+"]");
    	System.out.println("password:["+password+"]");
    }
}
